package com.thot.timyag.common;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import com.thot.timyag.exceptions.ThotException;
import com.thot.timyag.exceptions.UninstallDepException;

/**
 * @author yangjf
 * @version 0.1.0
 * @time 2019/4/12
 */
public class FileUtil {

    private static final String DEFAULT_DECIMAL_FROMAT = "0.00";
    private static final int CONSTANT_1024 = 1024;
    private static final String UNIT_GB = "GB";
    private static final String UNIT_MB = "MB";
    private static final String UNIT_KB = "KB";
    private static final String UNIT_B = "B";
    private static int DEFAULT_GC_COUNT = 10;

    public static void saveFile(byte[] bytes, File targetFile) throws IOException {
        BufferedOutputStream stream = null;
        try {
            stream = new BufferedOutputStream(new FileOutputStream(targetFile));
            stream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (null != stream) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        }
    }

    public static File saveFile(byte[] bytes, String originalFilename, String path) throws IOException {
        File targetFile = new File(path +
                File.separator + originalFilename);
        saveFile(bytes, targetFile);
        return targetFile;
    }

    public static void deleteFile(String fileName, String path) {
        File targetFile = new File(path +
                File.separator + fileName);
        deleteFile(targetFile);
    }

    public static void deleteFile(File file) {
        boolean deleteFlag = true;
        if (null != file && file.exists()) {
            deleteFlag = file.delete();
            int tryCount = 0;
            while (!deleteFlag && tryCount++ < DEFAULT_GC_COUNT) {
                System.out.println("GC for delete file");
                System.gc(); // 回收资源
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                deleteFlag = file.delete();
            }
        }
        if (!deleteFlag) {
            throw new UninstallDepException("uninstall file error");
        }
    }

    public static String getFileSize(File file) {
        DecimalFormat df = new DecimalFormat(DEFAULT_DECIMAL_FROMAT);
        if (file.exists()) {
            double bytes = file.length();
            double kilobytes = (bytes / CONSTANT_1024);
            double megabytes = (kilobytes / CONSTANT_1024);
            double gigabytes = (megabytes / CONSTANT_1024);

            if (gigabytes > 1) {
                //返回 单位GB
                return df.format(gigabytes) + UNIT_GB;
            } else if (megabytes > 1) {
                //返回单位 MB
                return df.format(megabytes) + UNIT_MB;
            } else if (kilobytes > 1) {
                //返回单位 KB
                return df.format(kilobytes) + UNIT_KB;
            } else {
                //返回单位 B
                return bytes + UNIT_B;
            }
        } else {
            throw new ThotException("文件不存在");
        }
    }
}
